﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.IO;

namespace z.Controllers {
	public class ImgController : Controller {
		[Authorize(Roles="Admin,Mod")]
		public ActionResult Administer() {
			DirectoryInfo di = new DirectoryInfo(Server.MapPath("~/Content/img/i/"));
			di.GetFiles("*.jpg").ToList();
			return View("Table", di.GetFiles("*.jpg").Select(x => new z.m.File(x.Name.Replace(x.Extension, ""))).ToList());
		}
		[Authorize(Roles = "Admin,Mod")]
		public ActionResult _Add() {
			var Out = new m.File("0");
			return View("_Row",Out);
		}
		[HttpPost]
		[Authorize(Roles = "Admin,Mod")]
		public ActionResult _Edit(m.File In) {
			if (In.Name == "0") {
				return null;
			} else {
				if (In.Name != In.OldName) {
					FileInfo f = new FileInfo(Server.MapPath("~/Content/img/i/" + In.OldName + ".jpg"));
					if (f.Exists)
						f.MoveTo(Server.MapPath("~/Content/img/i/" + In.Name + ".jpg"));
				}
				if (Request.Files["file"] != null && Request.Files["file"].ContentType == "image/jpeg") {
					Request.Files["file"].SaveAs(Server.MapPath("~/Content/img/i/" + In.Name + ".jpg"));
				}
				In.OldName = In.Name;
			}
			return View("_Row", In);
		}
		public ActionResult Index(string src) {
			ViewBag.url = Url.Content("~/Content/css/img/icon/" + src+".png");
			Response.ContentType = "image/svg+xml";
			return View();
		}
	}
}
